#include "Log.hpp"
#include "Socket.hpp"
#include <sys/epoll.h>
#include <iostream>
#include <cstring>
#include "nocopy.hpp"

using namespace std;

const int size = 1314;

class Epoller:public nocopy
{
public:
    Epoller()
    {
        _epfd = epoll_create(size);
        if(_epfd >= 0)
        {
            lg(Info,"epoll create success,fd: %d",_epfd);
        }
        else
        {
            lg(Error,"epoll create fail");
        }
    }

    int EpollerWait(struct epoll_event revents[],int maxnum)
    {
        int n = epoll_wait(_epfd,revents,maxnum,_timeout);
        return n;
    }

    int EpollCtl(int op,int fd,uint32_t events)
    {
        //记住如果op是EPOLL_CTL_DEL，传入的事件可以为nullptr
        struct epoll_event event;
        //为什么还要添加fd到event？？？
        event.data.fd = fd;        
        event.events = events;

        int n = epoll_ctl(_epfd,op,fd,&event);

        if(n != 0)
        {
            lg(Error,"Ctl error occurs: %s",strerror(errno));
        }

        return n;
    }

    ~Epoller()
    {
        if(_epfd >= 0) close(_epfd);
    }
private:
    int _epfd;
    int _timeout{3000};
};